package com.ruoyi.business.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.ruoyi.business.domain.ServiceItem;
import com.ruoyi.business.domain.Statement;
import com.ruoyi.business.domain.StatementItem;
import com.ruoyi.business.mapper.ServiceItemMapper;
import com.ruoyi.business.service.IServiceItemService;
import com.ruoyi.business.service.IStatementItemService;
import com.ruoyi.business.service.IStatementService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

import javax.security.auth.login.LoginException;
import javax.sql.rowset.serial.SerialException;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-01-24
 */
@Service
@Transactional
public class ServiceItemServiceImpl implements IServiceItemService {
    @Autowired
    private ServiceItemMapper serviceItemMapper;
    @Autowired
    private IStatementService statementService;
    @Autowired
    private IStatementItemService statementItemService;

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public ServiceItem selectServiceItemById(Long id)
    {
        return serviceItemMapper.selectServiceItemById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param serviceItem 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<ServiceItem> selectServiceItemList(ServiceItem serviceItem) {
        serviceItem.setCarPackage(null); //清空套餐
        serviceItem.setAuditStatus(null);//清空审核
        return serviceItemMapper.selectServiceItemList(serviceItem);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param serviceItem 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertServiceItem(ServiceItem serviceItem)
    {
        serviceItem.setCreateTime(DateUtils.getNowDate());
        return serviceItemMapper.insertServiceItem(serviceItem);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param serviceItem 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateServiceItem(ServiceItem serviceItem)
    {
        return serviceItemMapper.updateServiceItem(serviceItem);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteServiceItemByIds(String ids)
    {
        return serviceItemMapper.deleteServiceItemByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteServiceItemById(Long id) {
        return serviceItemMapper.deleteServiceItemById(id);
    }

    /**
     * 软删除
     * @param id 服务单项id
     */
    @Override
    public void updateByDelete(Long id) {
        if (id == null) {
            throw new ServiceException("参数传递错误!");
        }
        ServiceItem serviceItem = serviceItemMapper.selectServiceItemById(id);
        if (ServiceItem.AUDITSTATUS_AUDITING.equals(serviceItem.getAuditStatus())){
            throw new ServiceException("非法操作");
        }

        serviceItemMapper.updateByDelete(id,ServiceItem.IS_DELETE_HIDE);
    }

    /**
     * 根据id和状态进行上下架处理
     * @param id 服务单项id
     * @param salestatus  上下架状态
     */
    @Override
    public void updateSaleStatus(Long id, Integer salestatus) {
        //查询单前id数据
        ServiceItem serviceItem = serviceItemMapper.selectServiceItemById(id);
        //AUDITSTATUS_APPROVED审核通过 , AUDITSTATUS_NO_REQUIRED无需审核
        Integer auditStatus = serviceItem.getAuditStatus();
        if (!(ServiceItem.AUDITSTATUS_APPROVED.equals(auditStatus) ||
                ServiceItem.AUDITSTATUS_NO_REQUIRED.equals(auditStatus))){
            throw new ServiceException("非法操作");
        }
        //合法操作
        //对相同状态下的相同操作直接结束方法
        Integer currenSales = serviceItem.getSaleStatus();
        if (currenSales.equals(salestatus)) {
            return;
        }
        //对合法的进行更新消息
        if (ServiceItem.SALESTATUS_OFF.equals(currenSales)
                && ServiceItem.CARPACKAGE_YES.equals(serviceItem.getCarPackage())){
            //下架后进行状态修改,如果是套餐进行审核的初始化
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_INIT);
        }
        serviceItem.setSaleStatus(salestatus);
        serviceItemMapper.updateServiceItem(serviceItem);
    }
    /**
     * 修改和新增的调用方法
     * @param serviceItem
     * @return
     */
    public ServiceItem setServiceItem(ServiceItem serviceItem){
        ServiceItem newServiceItem = new ServiceItem();
        if (serviceItem.getId() != null) {
            newServiceItem.setId(serviceItem.getId());
        }
        newServiceItem.setName(serviceItem.getName());
        newServiceItem.setOriginalPrice(serviceItem.getOriginalPrice());
        newServiceItem.setDiscountPrice(serviceItem.getDiscountPrice());
        newServiceItem.setCarPackage(serviceItem.getCarPackage());
        newServiceItem.setInfo(serviceItem.getInfo());
        newServiceItem.setCreateTime(new Date());
        newServiceItem.setServiceCatalog(serviceItem.getServiceCatalog());
        newServiceItem.setSaleStatus(ServiceItem.SALESTATUS_OFF);//上架状态重置
        Integer carPackage = serviceItem.getCarPackage();//是否套餐
        newServiceItem.setCarPackage(carPackage);
        if (ServiceItem.CARPACKAGE_NO.equals(carPackage)) {
            //不是套餐,进行无需审核
            newServiceItem.setAuditStatus(ServiceItem.AUDITSTATUS_NO_REQUIRED);
        } else {
            //是套餐,审核初始化
            newServiceItem.setAuditStatus(ServiceItem.AUDITSTATUS_INIT);
        }
        return newServiceItem;
    }
    //服务单项新增
    @Override
    public void saverServiceItem(ServiceItem serviceItem) {
        //对服务进行初始化
        //1.根据套餐进行初始化审核状态
        ServiceItem newServiceItem = setServiceItem(serviceItem);
        serviceItemMapper.insertServiceItem(newServiceItem);
    }

    //服务单项编辑
    @Override
    public void update(ServiceItem serviceItem) {
        ServiceItem item = serviceItemMapper.selectServiceItemById(serviceItem.getId());
        if (ServiceItem.AUDITSTATUS_AUDITING.equals(item.getAuditStatus())){
            throw new ServiceException("非法操作");
        }
        //对服务进行初始化
        //1.根据套餐进行初始化审核状态
        ServiceItem newServiceItem = setServiceItem(serviceItem);
        serviceItemMapper.updateServiceItem(newServiceItem);
    }

    @Override
    public void saveItems(List<StatementItem> saveItems) {
        if (saveItems == null){
            throw new ServiceException("内容不能为空");
        }
        //获取对象数据内容
        StatementItem removeStatementItem = saveItems.remove(saveItems.size() - 1);//获取最后添加对象数据
        BigDecimal itemPrice = removeStatementItem.getItemPrice();//获取优惠金额
        Long statementId = removeStatementItem.getStatementId(); //获取客户id
        //对支付状态进行判断
        Statement statement = statementService.selectStatementById(statementId);
        if (Statement.STATUS_PAID.equals(statement.getStatus())){
            throw new ServiceException("该订单押金支付");
        }
        //对之前的保存内容进行删除
        statementItemService.deleteByStatemenyId(statementId);

        //对需要保存的进行保存
        BigDecimal totalAmount = new BigDecimal("0");
        BigDecimal totalQuantity = new BigDecimal("0");
        if (saveItems.size()>0){
            for (StatementItem saveItem : saveItems) {
                statementItemService.insertStatementItem(saveItem);
                totalAmount = totalAmount.add(saveItem.getItemPrice().multiply(saveItem.getItemQuantity()));
                totalQuantity = totalQuantity.add(saveItem.getItemQuantity());
            }
        }
        //更新结算单信息
        //如果优惠金额比总金额还要更多，属于非法逻辑
        if(totalAmount.compareTo(itemPrice)<0){
            throw new ServiceException("非法操作,优惠金额大于总金额");
        }
        statementService.updateAmount(statement.getId(),totalAmount,totalQuantity,itemPrice);
    }

}
